// 简单的SpeedStatistics功能验证
// 模拟SpeedStatistics类的JavaScript版本进行测试

class SpeedStatistics {
  constructor() {
    this.uploadStartTime = 0;
    this.bytesSent = 0;
    this.uploadAvg = 0;
    this.speeds = [];
    this.sum = 0;
    this.max = Number.MIN_VALUE;
    this.min = Number.MAX_VALUE;
    this.reset();
  }

  reset() {
    this.bytesSent = 0;
    this.speeds = [];
    this.sum = 0;
    this.max = Number.MIN_VALUE;
    this.min = Number.MAX_VALUE;
  }

  update(size, speed) {
    this.bytesSent += size;
    
    // 计算平均上传速度
    const currentTime = Date.now();
    const elapsedTimeSeconds = (currentTime - this.uploadStartTime) / 1000.0;
    this.uploadAvg = this.bytesSent / elapsedTimeSeconds;
    
    // 添加当前速度到数组
    this.speeds.push(speed);
    this.sum += speed;
    
    // 更新最大最小值
    if (this.max < speed) {
      this.max = speed;
    }
    if (this.min > speed) {
      this.min = speed;
    }
  }

  avg() {
    if (this.speeds.length === 0) {
      return 0;
    }
    return this.sum / this.speeds.length;
  }
}

// 测试SpeedStatistics类
console.log("Testing SpeedStatistics class...");

const stats = new SpeedStatistics();
stats.uploadStartTime = Date.now();

// 模拟一些数据更新
setTimeout(() => {
  stats.update(1024, 100);
  console.log("After first update:");
  console.log("- Bytes sent:", stats.bytesSent);
  console.log("- Max speed:", stats.max);
  console.log("- Min speed:", stats.min);
  console.log("- Avg speed:", stats.avg());
}, 100);

setTimeout(() => {
  stats.update(2048, 150);
  console.log("After second update:");
  console.log("- Bytes sent:", stats.bytesSent);
  console.log("- Max speed:", stats.max);
  console.log("- Min speed:", stats.min);
  console.log("- Avg speed:", stats.avg());
}, 200);

setTimeout(() => {
  stats.update(1536, 80);
  console.log("After third update:");
  console.log("- Bytes sent:", stats.bytesSent);
  console.log("- Max speed:", stats.max);
  console.log("- Min speed:", stats.min);
  console.log("- Avg speed:", stats.avg());
  console.log("- Upload avg:", stats.uploadAvg);
}, 300);

setTimeout(() => {
  console.log("Testing reset...");
  stats.reset();
  console.log("After reset:");
  console.log("- Bytes sent:", stats.bytesSent);
  console.log("- Speeds array length:", stats.speeds.length);
  console.log("- Avg speed:", stats.avg());
}, 400);

console.log("SpeedStatistics test completed!");